package model;

import info.bliki.api.Page;
import info.bliki.api.User;
import info.bliki.wiki.filter.PlainTextConverter;
import info.bliki.wiki.model.WikiModel;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;


public class Model {
	
	private Map<Integer, Book> mapBooks = new HashMap<Integer, Book>();
	private Map<Integer, Customer> mapCustomers = new HashMap<Integer, Customer>();
	private Map<Integer, Comment> mapComments = new HashMap<Integer, Comment>();
	private Map<Integer, Grade> mapGrades = new HashMap<Integer, Grade>();
	private ArrayList<Comment> arraylistCommentsDisplayed = new ArrayList<Comment>();
	
	
	public Model(){
		
	}
	

	public Map<Integer, Book> getMapBooks() {
		return mapBooks;
	}


	public void setMapBooks(Map<Integer, Book> mapBooks) {
		this.mapBooks = mapBooks;
	}


	public Map<Integer, Customer> getMapCustomers() {
		return mapCustomers;
	}


	public void setMapCustomers(Map<Integer, Customer> mapCustomers) {
		this.mapCustomers = mapCustomers;
	}


	public Map<Integer, Comment> getMapComments() {
		return mapComments;
	}


	public void setMapComments(Map<Integer, Comment> mapComments) {
		this.mapComments = mapComments;
	}


	public Map<Integer, Grade> getMapGrades() {
		return mapGrades;
	}


	public void setMapGrades(Map<Integer, Grade> mapGrades) {
		this.mapGrades = mapGrades;
	}


	public ArrayList<Comment> getArraylistCommentsDisplayed() {
		return arraylistCommentsDisplayed;
	}


	public void setArraylistCommentsDisplayed(
			ArrayList<Comment> arraylistCommentsDisplayed) {
		this.arraylistCommentsDisplayed = arraylistCommentsDisplayed;
	}


	public void updateArrayListCommentsDisplayed(int idBook){
		
		arraylistCommentsDisplayed.clear();
		
		Set cles = mapComments.keySet();
        Iterator it = cles.iterator();
        while (it.hasNext()){
           Object cle = it.next();
           Comment valeur = mapComments.get(cle);

           if(valeur.getIdBook() == idBook){
        	   arraylistCommentsDisplayed.add(valeur);
           }
        }
	}
	
	
	
	//http://www.integratingstuff.com/2012/04/06/hook-into-wikipedia-using-java-and-the-mediawiki-api/
    //http://code.google.com/p/gwtwiki/
    //http://code.google.com/p/gwtwiki/wiki/WikiModels
    //http://code.google.com/p/gwtwiki/wiki/Mediawiki2HTML
    
    public String getWikiData(String bookTitle) {
    	String[] listOfTitleStrings = { bookTitle };
    	String html = new String();
    	String plainStr = new String();
    	User user = new User("", "", "http://en.wikipedia.org/w/api.php");
    	user.login();
    	List<Page> listOfPages = user.queryContent(listOfTitleStrings);
    	
    	for (Page page : listOfPages) {
    		
    		WikiModel wikiModel = new WikiModel("${image}", "${title}");
    		html = wikiModel.render(page.toString());
    		
    		String delimiterhtml = "<table id=\"toc\" class=\"toc\" summary=\"Contents\">";
    		String strhtml[] = html.split(delimiterhtml);
    		html = strhtml[0];
    		String delimiterhtml2 = "Content:";
    		String strhtml2[] = html.split(delimiterhtml2);
    		html = strhtml2[1];
    		
            plainStr = wikiModel.render(new PlainTextConverter(), html);
            
    	}
    	return plainStr;
    }

}

